// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var beiMiCommon = require("BeiMiCommon");
cc.Class({
    extends: beiMiCommon,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        chatView:{
            default:null,
            type:cc.ScrollView
        },
        msgRow:{
            default:null,
            type:cc.Prefab
        },
        msgRowSelf:{
            default:null,
            type:cc.Prefab
        },
        msgInput:{
            default:null,
            type:cc.EditBox
        },
        totalCount:0
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        // for(let i=0;i<this.totalCount;i++){
        //     var msgRow = cc.instantiate(this.msgRow);
        //     this.chatView.content.addChild(msgRow);
        // }
        

    },
    showChat(){
        this.node.active=true;
        console.log("showChat");
    },
    hideChat(){
        this.node.active=false;
        console.log("hideChat");
    },
    sendMsg(){
       var msg = this.msgInput.string;
       this.msgInput.string="";
        var msgRow = cc.instantiate(this.msgRowSelf);
        var msgScript = msgRow.getComponent("msg");
        //设置聊天文字
        msgScript.msg_txt.string=msg;
        //设置昵称
        msgScript.nickname.string = cc.beimi.user.username;

        this.chatView.content.addChild(msgRow);
        let socket = this.socket();
        socket.emit("roomMsg",msg);

    },
    showMsg(data){
         var msgRow = cc.instantiate(this.msgRow);
         var msgScript = msgRow.getComponent("msg");

         //设置聊天文字
         msgScript.msg_txt.string=data.roomMsg;
        //设置昵称
        msgScript.nickname.string = data.player.username;
        //设置聊天头像
        // msgScript.bg_head = 

         this.chatView.content.addChild(msgRow);
    },
    start () {

    },

    // update (dt) {},
});
